<div class="p-3 border-x">
    <span class="layui-btn layui-btn-sm" id="setPartyBtn">+ 设置当事人</span>
</div>
<div>
    <table class="layui-hide" id="partyTable" lay-filter="partyTable"></table>
</div>
<input type="hidden" id="case_id" value="{$id}">

<script type="text/html" id="operateBar">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">移除</a>
    <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="setRole">委托设置</a>
    <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="setRelationship">诉讼身份</a>
</script>

<script>
    const moduleInit = ['tool', 'admin'];
    function partyInit() {
        if($('#caseTab').find('li').eq(1).data('load') == 'true'){
            return false;
        }
        $('#caseTab').find('li').eq(1).data('load','true');
        
        layui.use(['table', 'form', 'tool', 'layer', 'oaPicker'], function(){
            var layer = layui.layer;
            var table = layui.table;
            var tool = layui.tool;
            var oaPicker = layui.oaPicker;

            // 初始化当事人表格
            table.render({
                elem: '#partyTable',
                url: '/lawcase/api/get_case_parties',
                width: '100%',
                where: {
                    case_id: $('#case_id').val()
                },
                cols: [[
                    {field: 'is_client', title: '委托身份', width: 120, align:'center', templet: function(d){
                        var icons = ['layui-icon-user', 'layui-icon-friends', 'layui-icon-group'];
                        return '<i class="layui-icon ' + icons[d.is_client-1] + '"></i> ' + d.is_client_text;
                    }},
                    {field: 'relationship', title: '诉讼身份', width: 120, align:'center', templet: function(d){
                        var icons = ['layui-icon-flag', 'layui-icon-survey', 'layui-icon-form', 'layui-icon-read', 'layui-icon-edit', 'layui-icon-list', 'layui-icon-util', 'layui-icon-template', 'layui-icon-set', 'layui-icon-app'];
                        return '<i class="layui-icon ' + icons[d.relationship] + '"></i> ' + d.relationship_text;
                    }},
                    {field: 'party_name', title: '当事人名称', width: 200},
                    {field: 'party_type', title: '类型', width: 80, align:'center', templet: function(d){
                        return d.party_type == 1 ? '个人' : '企业';
                    }},
                    {field: 'id_number', title: '证件号码', width: 200},
                    {field: 'contact', title: '联系人', width: 100, align:'center'},
                    {field: 'phone', title: '联系电话', width: 120, align:'center'},                    
                    {fixed: 'right', title: '操作', width: 250, align:'center', toolbar: '#operateBar'}

                ]],
                page: true
            });

            // 设置当事人按钮事件
            $('#setPartyBtn').click(function(){
                layui.use(['tool', 'oaPicker', 'layer'], function(){
                    let tool = layui.tool, oaPicker = layui.oaPicker, layer = layui.layer;
                    
                    let opts = {
                        title: '选择当事人',
                        url: '/lawcase/api/get_all_partys',
                        area: ['800px', '500px'],
                        cols: [
                            {field: 'name', title: '当事人名称', width: 150},
                            {field: 'party_type_text', title: '类型', width: 100, align:'center'},
                            {field: 'phone', title: '联系电话', width: 150, align:'center'},
                            {field: 'full_address', title: '地址', width: 300}
                        ],
                        valueField: 'id',
                        add: '/lawcase/party/add',
                        page: true,
                        search: true

                    };
                    
                    window.currentLayerIndex = oaPicker.picker(opts, 1, function(selectData){
                        if(selectData.length > 0) {
                            let callback = function(e) {
                                layer.msg(e.msg);
                                if(e.code === 0) {
                                    table.reload('partyTable');
                                }
                                window.currentLayerIndex = null;
                            };
                            
                            if (!selectData[0] || !selectData[0].id) {
                                layer.msg('请选择有效的当事人');
                                return;
                            }
                            
                            tool.post("/lawcase/api/link_party_case", {
                                case_id: $('#case_id').val(),
                                party_id: selectData[0].id,
                                name: selectData[0].name,
                                party_type: selectData[0].type,
                                phone: selectData[0].phone
                            }, callback);
                        }
                    });
                });
            });

            // 表格操作事件
            table.on('tool(partyTable)', function(obj){
                var data = obj.data;
                if(obj.event === 'del'){
                    layer.confirm('确认删除该当事人？', function(index){
                        tool.post('/lawcase/api/del_party_case', {
                            id: data.id,
                            case_id: $('#case_id').val()
                        }, function(res) {
                            layer.msg(res.msg);
                            if(res.code === 0) {
                                table.reload('partyTable');
                            }
                            layer.close(index);
                        });
                    });
                } else if(obj.event === 'setRole') {
                    layer.open({
                        title: '设置委托身份',
                        content: '<div class="layui-form" style="padding:20px;">\n' +
                            '<div class="layui-form-item">\n' +
                            '    <label class="layui-form-label">委托身份</label>\n' +
                            '    <div class="layui-input-block">\n' +
                            '        <select name="is_client" lay-verify="required">\n' +
                            '            <option value="1">委托人</option>\n' +
                            '            <option value="2">对方当事人</option>\n' +
                            '            <option value="3">第三人</option>\n' +
                            '        </select>\n' +
                            '    </div>\n' +
                            '</div>\n' +
                            '</div>',
                        btn: ['确定', '取消'],
                        area: ['500px', '300px'],
                        success: function(layero, index) {
                            layui.form.render();
                        },
                        yes: function(index, layero) {
                            var is_client = $(layero).find('select[name="is_client"]').val();
                            tool.post('/lawcase/api/set_party_role', {
                                id: data.id,
                                case_id: $('#case_id').val(),
                                is_client: is_client
                            }, function(res) {
                                layer.msg(res.msg);
                                if(res.code === 0) {
                                    table.reload('partyTable');
                                }
                                layer.close(index);
                            });
                        }
                    });
                } else if(obj.event === 'edit') {
                    layer.open({
                        title: '编辑当事人',
                        type: 2,
                        area: ['800px', '90%'],
                        content: '/lawcase/party/add?id=' + data.party_id,
                        success: function(layero, index) {
                            // 窗口打开后回调
                        },
                        end: function() {
                            // 窗口关闭后刷新表格
                            table.reload('partyTable');
                        }
                    });
                } else if(obj.event === 'setRelationship') {
                    layer.open({
                        title: '设置诉讼身份',
                        content: '<div class="layui-form" style="padding:20px;">\n' +
                            '<div class="layui-form-item">\n' +
                            '    <label class="layui-form-label">诉讼身份</label>\n' +
                            '    <div class="layui-input-block">\n' +
                            '        <select name="relationship" lay-verify="required">\n' +
                            '            <option value="0">未知</option>\n' +
                            '            <option value="1">原告</option>\n' +
                            '            <option value="2">被告</option>\n' +
                            '            <option value="3">第三人</option>\n' +
                            '            <option value="4">上诉人</option>\n' +
                            '            <option value="5">被上诉人</option>\n' +
                            '            <option value="6">再审申请人</option>\n' +
                            '            <option value="7">再审被申请人</option>\n' +
                            '            <option value="8">申请执行人</option>\n' +
                            '            <option value="9">被申请执行人</option>\n' +
                            '        </select>\n' +
                            '    </div>\n' +
                            '</div>\n' +
                            '</div>',
                        btn: ['确定', '取消'],
                        area: ['500px', '500px'],
                        success: function(layero, index) {
                            layui.form.render();
                        },
                        yes: function(index, layero) {
                            var relationship = $(layero).find('select[name="relationship"]').val();
                            tool.post('/lawcase/api/set_party_relationship', {
                                id: data.id,
                                case_id: $('#case_id').val(),
                                relationship: relationship
                            }, function(res) {
                                layer.msg(res.msg);
                                if(res.code === 0) {
                                    table.reload('partyTable');
                                }
                                layer.close(index);
                            });
                        }
                    });
                }
            });
        });
    }
</script>